package outsideInterface.yxc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import framework.common.Common;
import framework.common.ResponseVo;
import framework.util.http._HttpUtil;
import framework.util.codeset.bo.SysCodesetDetail;
import framework.util.codeset.service.SysCodesetDetailService;
import framework.util.controller.BaseController;
import framework.util.layui._LayuiUtil;
import framework.util.queryFilter.service.impl.QueryFilter;
import framework.util.queryFilter.vo.Where;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 为小程序或手机端等其他端口提供外部接口。目前还没想到解决外部接口被恶意攻击的方法
 * @author 吴宇亮 on 2019/7/27 8:03
 */
@Controller
@RequestMapping("/outsideInterface/YxcOutsideInterface.do")
public class YxcOutsideInterface extends BaseController {

//    @Resource
//    private SysCodesetDetailService sysCodesetDetailService;
//
//    /**
//     * 小程序获取商品信息接口
//     */
//    @ResponseBody
//    @RequestMapping(params = "method=getYxcGoods")
//    public ResponseVo getYxcGoods(ModelAndView model, HttpServletRequest request, HttpServletResponse response){
//        String q_barcode = request.getParameter("barcode");
//        if(StringUtils.isBlank(q_barcode)){
//            return sendFail("barcode不能为空!");
//        }
//
//        QueryFilter filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
//        filter.where(YxcGoods.BARCODE, Where.eq, q_barcode);
//        List<YxcGoods> list = yxcGoodsService.getAll(filter);
//        if(list.size() == 0){ //请求接口，将数据同步到本地在返回给前端
//            filter = new QueryFilter(_LayuiUtil.FIRST_PAGE, Integer.MAX_VALUE);
//            filter.where(SysCodesetDetail.K, Where.eq, "isAllowedAddYxcGoodsByWx");
//            SysCodesetDetail setting = sysCodesetDetailService.get(filter);
//            if(!Common.TRUE.equals(setting.getV())){
//                return sendFail("只支持查询【裕兴昌商品】的价格");
//            }
//
//            try {
//                String res = _HttpUtil.doGet(Common.GET_GOODS_MSG_BY_BARCODE_URL + q_barcode);
//                JSONObject jsonObject = JSON.parseObject(res);
//                String code = jsonObject.get("code").toString();
//                if(!"1".equalsIgnoreCase(code)){
//                    String msg = jsonObject.get("msg").toString();
//                    return sendFail(msg);
//                }
//
//                jsonObject = JSON.parseObject(jsonObject.get("data").toString());
//                //商品名称、商品对应的条形码code、预估价格、品牌、厂商、规格
//                String goodsName = jsonObject.get("goodsName").toString();
//                String barcode = jsonObject.get("barcode").toString();
//                String advicePrice = jsonObject.get("price").toString();
//                String brand = jsonObject.get("brand").toString();
//                String supplier = jsonObject.get("supplier").toString();
//                String spec = jsonObject.get("standard").toString();
//
//                YxcGoods bo = new YxcGoods();
//                bo.setOfficialName(goodsName);
//                bo.setCommonName(goodsName);
//                bo.setBarcode(barcode);
//                bo.setAdvicePrice(advicePrice);
//                bo.setBrand(brand);
//                bo.setSupplier(supplier);
//                bo.setUnit("");
//                bo.setSpec(spec);
//                yxcGoodsService.add(bo);
//                return sendSuccess(bo);
//            } catch (IOException e) {
//                e.printStackTrace();
//                return sendError("服务器IO异常");
//            }
//        }else{
//            YxcGoods yxcGoods = list.get(0);
//            return sendSuccess(yxcGoods);
//        }
//    }
}
